<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2025/6/20
 * Time: 15:09
 */

namespace app\controller\Sys;
use app\model\Nav AS model;
use app\model\InfoContent AS content;
use app\controller\Base;
use think\facade\Db;

class Nav extends Base{
    public function list(){
        $where = [];
        $post = input('post.');
        !empty($post['name']) && $where[] = ['name','like','%'.$post['name'].'%'];
        $list = model::where($where)
            ->select();
        $list = getTree($list);
        return $this->msg($list);
    }

    public function info($id){
        $find = model::find($id);
        $content = content::find($find->content_id);
        if (empty($content->id))
            $find->content = '';
        else
            $find->content = $content->content;
        return $this->msg($find);
    }

    public function save(){
        try {
            Db::startTrans();
            $id = input('id',0);
            $post = input('post.');
            $post['uniqid'] = file_get_contents('key.log');
            $count = model::where('url',$post['url'])->where('id','<>',$id)->count();
            $post['url'] = str_replace(' ','_',$post['url']);
            if ($count > 0)
                $post['url'] = $post['url'].'_'.rand(1000,9999);
            if (!empty($post['images'])){
                foreach ($post['images'] AS $key => $value)
                    \app\model\Image::where('id',$value['id'])->save($value);
                $images_id = json_encode(array_column($post['images'],'id'));
                $post['images_id'] = $images_id;
                unset($post['images']);
            }
            $web = \app\model\Web::where('')->value('web');
            $post['url'] = $web.'/'.$post['url'];
            if (empty($id)) {
                if (!empty($post['content'])) {
                    $content = content::create(['content' => $post['content']]);
                    $post['content_id'] = $content->id;
                }
                $find = model::create($post);
            } else {
                $find = model::find($id);
                if (!empty($post['content'])) {
                    $content = content::find($find->content_id);
                    if (!empty($content->id))
                        $content->save(['content' => $post['content']]);
                    else
                        $content = content::create(['content' => $post['content']]);
                    $post['content_id'] = $content->id;
                }
                if (empty($find->id))
                    throw new \Exception('数据不存在');
                $find->save($post);
            }
            $route = file_get_contents('../route/app.php');
            $start = strpos($route, '//开始路由');
            $end = strpos($route, '//结束路由');
            $comment = substr($route, $start, $end - $start + strlen('//结束路由'));
            $select = model::select();
            $string = "//开始路由\n";
            foreach ($select AS $key => $value){
                $string .= "Route::rule('{$value->url}','index');\n";
            }
            $string .= "//结束路由";
            $route = str_replace($comment,$string,$route);
            file_put_contents('../route/app.php',$route);
            Db::commit();
            return $this->msg(['find' => $find,'route' => $route]);
        }catch (\Exception $e){
            Db::rollback();
            return $this->fail($e->getMessage());
        }
    }

    public function delete(){
        $id = input('id');
        $find = model::find($id);
        if (empty($find->id))
            return $this->fail('数据不存在');
        $find->delete();
        return $this->msg();
    }
}